<?php

namespace App\Models;

class ProdOrder extends Base
{
    //分页获取列表数据
    public function getPageList($options=[],$where=[],$whereDates,$prodtype=1){
        try{
            $limit = (isset($options['limit']) && $options['limit']) ? $options['limit'] : 1;
            $page = (isset($options['page']) && $options['page']) ? $options['page'] : 1;
            $model = $this->leftJoin('user_addresses','user_addresses.id','=','prod_orders.addressid');
            if($prodtype==2){ //积分商品
                $model = $model->leftJoin('jifen_products','jifen_products.id','=','prod_orders.proid')
                         ->select('prod_orders.*','jifen_products.image','jifen_products.proname','jifen_products.proname_zn','jifen_products.coin','jifen_products.price','user_addresses.username','user_addresses.contact','user_addresses.address');
            }else{ //商品
                $model = $model->leftJoin('products','products.id','=','prod_orders.proid')
                         ->select('prod_orders.*','products.image','products.proname','products.proname_zn','products.rprice','user_addresses.username','user_addresses.contact','user_addresses.address');
            }
            if(!empty($whereDates)){
                $model = $model->whereDate('prod_orders.created_at','>=',$whereDates[0])->whereDate('prod_orders.created_at','<',$whereDates[1]);
            }
            $model=$model->where($where)->orderBy('prod_orders.created_at','desc');
            $result = $this->paging($model,$page,$limit);
            return $result;
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }
    }

    //前台添加商品订单
    public function oneInsert($data=array(),$token,$lang="zn"){
        try {
            $user = (new User())->where('token',$token)->select('id','total','balance','cycle_balance','deleted_at')->first();
            if($user->deleted_at){
                return [
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ];
            }
            //获取商品价格
            $deleted_at= (new Product())->where('id',$data['proid'])->value('deleted_at');
            if($deleted_at){
                return [
                    'code'=>1,
                    'message'=>$this->lang['PROD_FORBIDDEN'][$lang]
                ];
            }
            $this->ordersn = date('YmdHis').mt_rand(1000,9999);
            $this->proprice = $data['proprice'];
            $this->status = 1;
            $this->uid = $user->id;
            $this->addressid = $data['addressid'];
            $this->fee = $data['fee'];
            $this->proid = $data['proid'];
            $this->pronum = $data['pronum'];
            $this->youhui = $data['youhui'];
            $this->sendtype = $data['sendtype'];
            $this->ordtype = $data['ordtype'];
            $this->remark = $data['remark'];
            $res = $this->save(); //添加订单
            if($res){
                $newtoken = $this->setToken($user->id);
                $user->token = $newtoken;
                $user->save();
                return [
                    'code'=>0,
                    'data'=>[
                        'id'=>$this->id,
                        'uid'=>$user->id,
                        'token'=>$newtoken
                    ]
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>$this->lang['ADD_FAIL'][$lang]
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }

    //前台添加积分商品订单
    public function insertJifen($data=array(),$token,$lang="zn"){
        try {
            $user = (new User())->where('token',$token)->select('id','total','balance','cycle_balance','deleted_at')->first();
            if($user->deleted_at){
                return [
                    'code'=>1,
                    'message'=>$this->lang['USER_FORBIDDEN'][$lang]
                ];
            }
            //获取商品价格
            $prod = (new JifenProduct())->where('id',$data['proid'])->select('price','coin','deleted_at')->first();
            $this->ordersn = date('YmdHis').mt_rand(1000,9999);
            $this->proprice = $prod->price;
            $this->procoin = $prod->coin;
            $this->status = 1;
            $this->uid = $user->id;
            $this->addressid = $data['addressid'];
            $this->fee = $data['fee'];
            $this->proid = $data['proid'];
            $this->pronum = $data['pronum'];
            $this->youhui = 0;
            $this->sendtype = $data['sendtype'];
            $this->ordtype = $data['ordtype'];
            $this->remark = $data['remark'];
            $res = $this->save(); //添加订单
            if($res){
                $newtoken = $this->setToken($user->id);
                $user->token = $newtoken;
                $user->save();
                return [
                    'code'=>0,
                    'data'=>[
                        'id'=>$this->id,
                        'moneys'=>$prod->price * $data['pronum'],
                        'coins'=>$prod->coin * $data['pronum'],
                        'uid'=>$user->id,
                        'token'=>$newtoken
                    ]
                ];
            }else{
                return [
                    'code'=>1,
                    'message'=>$this->lang['ADD_FAIL'][$lang]
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$this->lang['ERROR'][$lang]
            ];
        }
    }

    //后台订单发货
    public function changeStatus($data=array()){
        try{
            $order = self::find($data['id']);
            $order->status = 3;
            $order->sendno = $data['sendno'];
            $res = $order->save();
            if($res){
                return [
                    'code'=>0,
                    'data'=>$data['id']
                ];
            }else{
                return [
                    'code'=>0,
                    'data'=>'发货失败，请重试'
                ];
            }
        }catch (\Exception $e){
            return [
                'code'=>1,
                'message'=>$e->getMessage()
            ];
        }

    }
}
